﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：UrlsRewrite.cs
  //文件功能描述：商城Url重写
  //创建人：赵国伟
  //生成时间：2012/03/19 20:50:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Shopping.Common;
using System.Text.RegularExpressions;

namespace Shopping.BLL
{

    public class URLRewrite : IHttpModule
    {
        private static List<RewriterRule> Rules = new List<RewriterRule>();
        public void Init(HttpApplication app)
        {
            app.AuthorizeRequest += new EventHandler(URLRewrite_AuthorizeRequest);

        }


        private void URLRewrite_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            LoadRules();
            string requestedPath = app.Request.Path;
            if (requestedPath.IndexOf(".html") >= 0)
            {
                foreach (RewriterRule rule in Rules)
                {
                    string lookFor = "^" + rule.LookFor + "$";
                    Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
                    if (re.IsMatch(requestedPath))
                    {
                        string s = re.Replace(requestedPath, rule.SendTo);
                        RewriteUrl(app.Context, s);
                        break;
                    }
                }

                string filePath = app.Context.Server.MapPath(requestedPath);
                app.Context.Response.Write(Utility.ReadFile(filePath));

            }
        }

        public void Dispose()
        {

        }

        #region "加载重写规则"
        /// <summary>
        /// 加载重写规则
        /// </summary>
        private void LoadRules()
        {
            if (Rules.Count > 0)
                return;

            RewriterRule r1 = new RewriterRule();
            r1.LookFor = @"/Goods/([0-9]+)\.html";
            r1.SendTo = Config.GoodsShowUrl + "?ID=$1";
            Rules.Add(r1);

            RewriterRule r2 = new RewriterRule();
            r2.LookFor = @"/List/([0-9]+)-0-0\.html";
            r2.SendTo = Config.GoodsListUrl + "?FCategoryID=$1";
            Rules.Add(r2);

            RewriterRule r3 = new RewriterRule();
            r3.LookFor = @"/List/([0-9]+)-([0-9]+)-([0-9]+)\.html";
            r3.SendTo = Config.GoodsListUrl + "?FCategoryID=$1&SCategoryID=$2&TCategoryID=$3";
            Rules.Add(r3);

            RewriterRule r4 = new RewriterRule();
            r4.LookFor = @"/List/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)\.html";
            r4.SendTo = Config.GoodsListUrl + "?FCategoryID=$1&SCategoryID=$2&TCategoryID=$3&GoodsPlaceID=$4&BrandID=$5&PackUnitID=$6&GoodsPriceCondID=$7&Sorts=$8&CurPage=$9";
            Rules.Add(r4);

            RewriterRule r5 = new RewriterRule();
            r5.LookFor = @"/List/([0-9]+)\.html";
            r5.SendTo = Config.GoodsListUrl + "?FCategoryID=$1";
            Rules.Add(r5);

            RewriterRule r6 = new RewriterRule();
            r6.LookFor = @"/News/([0-9]+)\.html";
            r6.SendTo = Config.NewsShowUrl + "?NewsID=$1";
            Rules.Add(r6);

            RewriterRule r7 = new RewriterRule();
            r7.LookFor = @"/Brands/([0-9]+)\.html";
            r7.SendTo = Config.GoodsListUrl + "?BrandID=$1";
            Rules.Add(r7);

            RewriterRule r8 = new RewriterRule();
            r8.LookFor = @"/NewProduct.html";
            r8.SendTo = "Search.aspx?CommendCategoryID=41";
            Rules.Add(r8);

            RewriterRule r9 = new RewriterRule();
            r9.LookFor = @"/Commends.html";
            r9.SendTo = "Search.aspx?CommendCategoryID=42";
            Rules.Add(r9);

            RewriterRule r10 = new RewriterRule();
            r10.LookFor = @"/Rush.html";
            r10.SendTo = "Search.aspx?CommendCategoryID=43";
            Rules.Add(r10);

            RewriterRule r11 = new RewriterRule();
            r11.LookFor = @"Dny.html";
            r11.SendTo = Config.GoodsListUrl + "?SpecialAreaID=28";
            Rules.Add(r11);


        }
        #endregion

        #region "执行url跳转"
        /// <summary>
        /// 执行url跳转
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sendToUrl"></param>
        private void RewriteUrl(HttpContext context, string sendToUrl)
        {
            // see if we need to add any extra querystring information
            if (context.Request.QueryString.Count > 0)
            {
                if (sendToUrl.IndexOf('?') != -1)
                    sendToUrl += "&" + context.Request.QueryString.ToString();
                else
                    sendToUrl += "?" + context.Request.QueryString.ToString();
            }

            // first strip the querystring, if any
            string sendToUrlLessQString;
            string queryString = String.Empty;
            sendToUrlLessQString = sendToUrl;
            if (sendToUrl.IndexOf('?') > 0)
            {
                sendToUrlLessQString = sendToUrl.Substring(0, sendToUrl.IndexOf('?'));
                queryString = sendToUrl.Substring(sendToUrl.IndexOf('?') + 1);
            }
            context.RewritePath(sendToUrlLessQString, String.Empty, queryString);
        }
        #endregion

        #region "URL重写的规则配置类；"
        /// <summary>
        /// URL重写的规则配置类；
        /// </summary>
        public class RewriterRule
        {
            /// <summary>
            /// 查找需要匹配的URL
            /// </summary>
            public string LookFor;
            /// <summary>
            /// 匹配后执行跳转的Url
            /// </summary>
            public string SendTo;
        }
        #endregion


    }
}
